package co.jp.roomy;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import co.jp.roomy.vo.Heya;
import co.jp.roomy.vo.MyKagu;
import co.jp.roomy.wheel.ArrayWheelAdapter;
import co.jp.roomy.wheel.OnWheelChangedListener;
import co.jp.roomy.wheel.OnWheelScrollListener;
import co.jp.roomy.wheel.WheelView;

public class SetKaguSizeActivity extends Activity implements OnClickListener {
	
	String wheelMenu[] = new String[]{"0","1","2","3","4","5","6","7","8","9"};
	// Wheel scrolled flag
	private boolean wheelScrolled = false;
	private TextView text;
    private Heya heya;
    private MyKagu myKaguCustom;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.set_kagu_size);
		
		Intent intent = getIntent();
		heya = (Heya)intent.getSerializableExtra(SetKaguSizeActivity.this.getString(R.string.heyaKey));
		myKaguCustom = (MyKagu)intent.getSerializableExtra(SetKaguSizeActivity.this.getString(R.string.myKaguCustomKey));

		initWheel(R.id.p1, myKaguCustom.getMyKaguSizeX1());
		initWheel(R.id.p2, myKaguCustom.getMyKaguSizeX2());
		initWheel(R.id.p3, myKaguCustom.getMyKaguSizeX3());
		initWheel(R.id.p4, myKaguCustom.getMyKaguSizeX4());
		initWheel(R.id.p5, myKaguCustom.getMyKaguSizeY1());
		initWheel(R.id.p6, myKaguCustom.getMyKaguSizeY2());
		initWheel(R.id.p7, myKaguCustom.getMyKaguSizeY3());
		initWheel(R.id.p8, myKaguCustom.getMyKaguSizeY4());
		initWheel(R.id.p9, myKaguCustom.getMyKaguSizeZ1());
		initWheel(R.id.p10, myKaguCustom.getMyKaguSizeZ2());
		initWheel(R.id.p11, myKaguCustom.getMyKaguSizeZ3());
		initWheel(R.id.p12, myKaguCustom.getMyKaguSizeZ4());
		
		text = (TextView) this.findViewById(R.id.result);
		Button button = (Button) findViewById(R.id.button);
        button.setText(R.string.button_ok);
        button.setOnClickListener(this);
        Button button02 = (Button) findViewById(R.id.button02);
        button02.setText(R.string.button_cancel);
	}
	
	@Override
	public void onClick(View view) {
		Intent intent = new Intent(SetKaguSizeActivity.this, MyKaguCustomActivity.class);
		myKaguCustom.setMyKaguSizeX1(Integer.valueOf(wheelMenu[getWheel(R.id.p1).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeX2(Integer.valueOf(wheelMenu[getWheel(R.id.p2).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeX3(Integer.valueOf(wheelMenu[getWheel(R.id.p3).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeX4(Integer.valueOf(wheelMenu[getWheel(R.id.p4).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeY1(Integer.valueOf(wheelMenu[getWheel(R.id.p5).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeY2(Integer.valueOf(wheelMenu[getWheel(R.id.p6).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeY3(Integer.valueOf(wheelMenu[getWheel(R.id.p7).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeY4(Integer.valueOf(wheelMenu[getWheel(R.id.p8).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeZ1(Integer.valueOf(wheelMenu[getWheel(R.id.p9).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeZ2(Integer.valueOf(wheelMenu[getWheel(R.id.p10).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeZ3(Integer.valueOf(wheelMenu[getWheel(R.id.p11).getCurrentItem()]));
		myKaguCustom.setMyKaguSizeZ4(Integer.valueOf(wheelMenu[getWheel(R.id.p12).getCurrentItem()]));
		
		intent.putExtra(getApplicationContext().getString(R.string.myKaguCustomKey), myKaguCustom);
		intent.putExtra(SetKaguSizeActivity.this.getString(R.string.heyaKey), heya);
		startActivity(intent);
	}
	public void finishDisp(View view) {
		finish();
	}
		// Wheel scrolled listener
	OnWheelScrollListener scrolledListener = new OnWheelScrollListener() {
			public void onScrollStarts(WheelView wheel)	{
					wheelScrolled = true;
			}
			public void onScrollEnds(WheelView wheel) {
				wheelScrolled = false;
				//updateStatus();
			}
	};

	// Wheel changed listener
	private final OnWheelChangedListener changedListener = new OnWheelChangedListener() {
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			if (!wheelScrolled) {
				//updateStatus();
			}
		}
	};

	/**
	 * Updates entered PIN status
	 */
	/**private void updateStatus() {
		text.setText("縦：" + wheelMenu[getWheel(R.id.p1).getCurrentItem()] + " , 横：" + wheelMenu[getWheel(R.id.p2).getCurrentItem()] + " , 高さ：" + wheelMenu[getWheel(R.id.p3).getCurrentItem()]);
	}*/

	/**
	 * Initializes wheel
	 * 
	 * @param id
	 *          the wheel widget Id
	 * @param integer 
	 */
	private void initWheel(int id, Integer integer) {
		WheelView wheel = (WheelView) findViewById(id);
		wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu));
		wheel.setVisibleItems(2);
		wheel.setCurrentItem(integer);
		wheel.addChangingListener(changedListener);
		wheel.addScrollingListener(scrolledListener);
	}

	private void initWheel2(int id) {
		WheelView wheel = (WheelView) findViewById(id);
		wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu));
		wheel.setVisibleItems(2);
		wheel.setCurrentItem(0);
		wheel.addChangingListener(changedListener);
		wheel.addScrollingListener(scrolledListener);
	}

	private void initWheel3(int id) {
		WheelView wheel = (WheelView) findViewById(id);
		wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu));
		wheel.setVisibleItems(2);
		wheel.setCurrentItem(0);
		wheel.addChangingListener(changedListener);
		wheel.addScrollingListener(scrolledListener);
	}

	/**
	 * Returns wheel by Id
	 * 
	 * @param id
	 *          the wheel Id
	 * @return the wheel with passed Id
	 */
	private WheelView getWheel(int id) {
		return (WheelView) findViewById(id);
	}

}